v = .45
first = 1

print "This program illustrates the basic operation of a slide rule"
print "for multiplication and division."
print "Type a multiply or divide problem as  nnnn * mmmm  or mmmm / nnnn:"
// Note: answers are rounded to 3 significant figures.
100
line input a$
if len(a$) = 0 then end
i = instr(a$, "*")
if i <> 0 then oper=1: goto 200 // multiply
i = instr(a$, "/")
if i = 0 then print "invalid operation": goto 100
oper = 0 // divide
200
a1$ = left$(a$, i-1)
a2$ = mid$ (a$, i+1)

if oper <> 1 then goto 300

// Multiply
a = val(a1$)
b = val(a2$)
c = a * b
signa = sgn(a)
signb = sgn(b)
la = log10(abs(a)): ea = exponent (a): lla = la - ea: 
m1 = exp10(lla)
lb = log10(abs(b)): eb = exponent (b): llb = lb - eb: 
m2 = exp10(llb)
st1$ =        str$(m1 * signa, " #.##") + " * 10^" + str$(ea) + "    *    "
st1$ = st1$ + str$(m2 * signb, " #.##") + " * 10^" + str$(eb)
lc = log10(abs(c)): ec = exponent(c): llc = lc - ec:
m3 = exp10(llc) 
st2$ = "    =    " + str$(m3 * signa * signb," #.##") + "  * 10^" + str$(ec)
goto 400

300 // Divide
c = val(a1$)
b = val(a2$)
a = c / b
signc = sgn(c)
signb = sgn(b)
lc = log10(abs(c)): ec = exponent (c): llc = lc - ec: 
m3 = exp10(llc)
lb = log10(abs(b)): eb = exponent (b): llb = lb - eb: 
m2 = exp10(llb)
la = log10(abs(a)): ea = exponent (a): lla = la - ea: 
m1 = exp10(lla)
st1$ =        str$(m3 * signc," #.##") + " * 10^" + str$(ec) + "    /    "
st1$ = st1$ + str$(m2 * signb," #.##") + " * 10^" + str$(eb)
st2$ = "    =    " + str$(m1 * signc * signb," #.##") + "  * 10^" + str$(ea)

400
if first <> 1 then graph end
first = 0
graph width=10,height=5, vscale=.2
graph noaxes, nogrid

if m1 * m2 > 10 then dir=-1 else dir = +1
if dir = -1 then goto 500

// C-scale is slid to the right of the D-scale.
graph hscale=(1.07+log10(m1))/10
ab = -1 // below
rc = draw_scale (0)
ab = +1 // above
rc = draw_scale (log10(m1))
//  draw the hairline
rc = draw_hairline (log10(m1) + log10(m2))
goto 600

500
// C-scale is slid to the left of the D-scale.
graph hscale=(2.07-log10(m1))/10
ab = -1 // below
rc = draw_scale (1-log10(m1))
ab = +1 // above
rc = draw_scale (0)
//  draw the hairline
rc = draw_hairline (log10(m2))


600  // print the "problem" on the graph

st3$ = st1$ + st2$
print st3$
text v=85,h=50,size=25,string=st3$,boxed

print "Type another problem or press 'enter' to quit:"
goto 100

//------------------------------------

def draw_scale (h) = gosub 9000
9000
if ab > 0 then shape bottom = v, top = v + .10, left = h -.02, right= h+1.02
if ab < 0 then shape bottom = v - .10, top = v, left = h -.02, right= h+1.02 

shape filltype=none, linecolor=(0,0,255),rectangle
shape linecolor=(0,0,0)
shape linestart = (h+log10(1),v), lineend = (h+log10(10),v), line

for n = 1 to 10 step 1
  rc = draw_long_tick (v,n)
next

rc = draw_numbers (v)

for n = 1 to 2 step .5
  rc = draw_long_tick (v,n)
next
for n = 1 to 2 step .1
  rc = draw_med_tick (v,n)
next
for n = 1 to 2 step .02
  rc = draw_short_tick (v,n)
next

for n = 2 to 5 step .5
  rc = draw_long_tick (v,n)
next
for n = 2 to 5 step .1
  rc = draw_med_tick (v,n)
next
for n = 2 to 5 step .05
  rc = draw_short_tick (v,n)
next

for n = 5 to 10 step .5
  rc = draw_med_tick (v,n)
next
for n = 5 to 10 step .1
  rc = draw_short_tick (v,n)
next
return

//-------------------------------

def draw_long_tick (v,n) = gosub 10000
10000 
shape linestart=(log10(n)+h, v), lineend=(log10(n)+h,v+.045*ab), line
return
def draw_med_tick (v,n) = gosub 10010

10010 
shape linestart=(log10(n)+h, v), lineend=(log10(n)+h,v+.03*ab), line
return
def draw_short_tick (v,n) = gosub 10020

10020 
shape linestart=(log10(n)+h, v),lineend=(log10(n)+h,v+.015*ab), line
return

def draw_numbers (v) = gosub 10030
10030
ds = .04
if ab < 0 then text_v = v-.075 else text_v = v+.05
stn1$="1"
text x=log10(1)+h, y=text_v,datasize=ds,string=stn1$
stn2$="2"
text x=log10(2)+h, y=text_v,datasize=ds,string=stn2$
stn3$="3"
text x=log10(3)+h, y=text_v,datasize=ds,string=stn3$
stn4$="4"
text x=log10(4)+h, y=text_v,datasize=ds,string=stn4$
stn5$="5"
text x=log10(5)+h, y=text_v,datasize=ds,string=stn5$
stn6$="6"
text x=log10(6)+h, y=text_v,datasize=ds,string=stn6$
stn7$="7"
text x=log10(7)+h, y=text_v,datasize=ds,string=stn7$
stn8$="8"
text x=log10(8)+h, y=text_v,datasize=ds,string=stn8$
stn9$="9"
text x=log10(9)+h,  y=text_v,datasize=ds,string=stn9$
text x=log10(10)+h, y=text_v,datasize=ds,string=stn1$
return

def draw_hairline (x) = gosub 10050
10050
shape linecolor=(255,0,0),linestart=(x,v+.15), lineend=(x,v-.15), line
shape linecolor=(0,0,0)
return